<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>透明度</title>
		<style>
			*{
				padding: 0%;
				margin: 0%;
				
			}
			#box{
				width: 200px;
				height: 200px;
				background-color: red;
				opacity: 0.3;
				filter: alpha:(opacity: 30);
			}
		</style>
	</head>
	<body>
		<div id="box"></div>
	</body>
	<script>
		window.onload = function() {
			let box = document.getElementById('box');
			let timer = null,alpha = 30,speed = 0;
			box.onmousemove = function() {
				opicityAnimation(this,100)
			}
			box.onmouseout = function() {
				opicityAnimation(this,30);
			}
			function opicityAnimation(obj,endAlpha) {
				clearInterval(timer);
				timer = setInterval(function(){
					speed = endAlpha > alpha ? 10 : -10;
					if(endAlpha === alpha) {
						clearInterval(timer);
						return;
					}
					alpha += speed;
					box.style.opacity = alpha / 100;
					box.style.filter = `alpha:(opacity: ${alpha})`;
				},30);
			}
		}
	</script>
</html>